package cn.edu.hzw.comic

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.fragment.app.Fragment
import cn.edu.hzw.comic.adapter.ViewPagerAdapter
import cn.edu.hzw.comic.mvp.view.HomeFragment
import cn.edu.hzw.comic.mvp.view.RankFragment
import cn.edu.hzw.comic.mvp.view.SortFragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*

class MainActivity : AppCompatActivity() {
    val strings:ArrayList<Int> = arrayListOf(R.string.tab_one,R.string.tab_two,R.string.tab_three)
    companion object{
        const val TYPE_MY: String = "my"
        const val TYPE_ROOM: String = "room"
        const val TYPE: String = "Type"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        toolbar.setTitle(R.string.toorbar_title)
        toolbar.title = resources.getString(R.string.toorbar_title)
        toolbar.subtitle = resources.getString(R.string.toorbar_subtitle)
        setSupportActionBar(toolbar)
        toolbar.navigationIcon = resources.getDrawable(R.mipmap.caver)
        val fragments = java.util.ArrayList<Fragment>()
        fragments.add(HomeFragment())
        fragments.add(RankFragment())
        fragments.add(SortFragment())
        val titles:List<String> = strings.map(this::getString)
        viewpager.adapter = ViewPagerAdapter(fragments,titles,supportFragmentManager)
        viewpager.offscreenPageLimit = 2
        tab.setupWithViewPager(viewpager)

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main,menu)
        return true
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.menu_my){
            val intent = Intent(this,AboutActivity().javaClass)
            intent.putExtra(TYPE,TYPE_MY)
            startActivity(intent)
            return true
        }
        if (id == R.id.menu_room){
            val intent = Intent(this,AboutActivity().javaClass)
            intent.putExtra(TYPE,TYPE_ROOM)
            startActivity(intent)
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
        if (menu!=null){
            if (menu.javaClass.simpleName == "MenuBuilder"){
                try {
                    val m = menu.javaClass.getDeclaredMethod(
                        "setOptionalIconsVisible", java.lang.Boolean.TYPE)
                    m.isAccessible = true
                    m.invoke(menu,true)
                }catch (e:NoSuchMethodException){

                }catch (e:Exception){
                    throw RuntimeException(e)
                }
            }
        }
        return super.onMenuOpened(featureId, menu)
    }
}